package com.zc.controller;

import com.zc.component.EntityFile;
import com.zc.entity.Fileer;
import com.zc.mapper.FileerMapper;
import com.zc.mapper.FilepathMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;


import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;


@Controller
public class DownFileController {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    EntityFile entityFile;

    @Autowired
    FilepathMapper filepathMapper;
    @Autowired
    FileerMapper fileerMapper;
    @ResponseBody
    @GetMapping ("/down/{id}")
    public void downloadPatrol(@PathVariable("id") int id,HttpServletResponse response,HttpSession session) {
        String name=fileerMapper.selectByPrimaryKey(id).getName();
        int teamid=(int)session.getAttribute("teamid");
        String path = entityFile.getBetweenRepository()+teamid+"/"+name;
        File  file = new File(path);
        if (!file.exists()) {
            logger.info("文件不存在,filePath={}",file);
            return;
        }

        FileInputStream fis = null;
        BufferedInputStream bis = null;
        try {
            response.setContentType("application/force-download");// 设置强制下载不打开
            response.addHeader("Content-Disposition", "attachment;fileName="+java.net.URLEncoder.encode(name, "UTF-8"));
            byte[] buffer = new byte[1024];
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            OutputStream outputStream = response.getOutputStream();
            int i = bis.read(buffer);
            while (i != -1) {
                outputStream.write(buffer, 0, i);
                i = bis.read(buffer);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @ResponseBody
    @GetMapping ("/down2/{id}")
    public void downloadPatrol2(@PathVariable("id") int id,HttpServletResponse response,HttpSession session) {
        Fileer fileer=fileerMapper.selectByPrimaryKey(id);
        String name=fileer.getName();
        String fileerpath=filepathMapper.selectByPrimaryKey(fileer.getFilepath()).getName();
        int teamid=(int)session.getAttribute("teamid");
        String path = entityFile.getRepository()+teamid+"/"+fileerpath+"/"+name;
        File  file = new File(path);
        if (!file.exists()) {
            logger.info("文件不存在,filePath={}",file);
            return;
        }

        FileInputStream fis = null;
        BufferedInputStream bis = null;
        try {
            response.setContentType("application/force-download");// 设置强制下载不打开
            response.addHeader("Content-Disposition", "attachment;fileName="+java.net.URLEncoder.encode(name, "UTF-8"));
            byte[] buffer = new byte[1024];
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            OutputStream outputStream = response.getOutputStream();
            int i = bis.read(buffer);
            while (i != -1) {
                outputStream.write(buffer, 0, i);
                i = bis.read(buffer);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
